package com.zjitc.common;

import sun.applet.resources.MsgAppletViewer;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/27 16:53
 * @description:
 */
public class ServerResponse<T> {
  /**
   *  response code
   */
  private Integer status;
  /**
   * response message
   * */
  private String msg;
  /**
   * response object
   */
  private T data;

  /**
   * create a server response object only with response code
   *
   * @param status ServerResponseCode.SUCCESS, ServerResponseCode.ERROR
   */
  private ServerResponse(Integer status) {
    this.status = status;
  }

  /**
   * create a server response object with response code and response message
   *
   * @param status ServerResponseCode.SUCCESS, ServerResponseCode.ERROR
   * @param msg response message
   */
  private ServerResponse(Integer status, String msg) {
    this.status = status;
    this.msg = msg;
  }

  /**
   * create a server response object with response code and response object
   *
   * @param status ServerResponseCode.SUCCESS, ServerResponseCode.ERROR
   * @param data response object
   */
  private ServerResponse(Integer status, T data) {
    this.status = status;
    this.data = data;
  }

  /**
   * create a server response object with response code, response message and response object
   *
   * @param status ServerResponseCode.SUCCESS, ServerResponseCode.ERROR
   * @param msg response message
   * @param data response object
   */
  private ServerResponse(Integer status, String msg, T data) {
    this.status = status;
    this.msg = msg;
    this.data = data;
  }

  /**
   * get response code
   *
   * @return ServerResponseCode.SUCCESS, ServerResponseCode.ERROR
   */
  public Integer getStatus() {
    return status;
  }

  /**
   * get response message
   *
   * @return response message
   */
  public String getMsg() {
    return msg;
  }

  /**
   * get response data
   *
   * @return response data
   */
  public T getData() {
    return data;
  }

  /**
   * current response is success
   *
   * @return success return true
   */
  public boolean success() {
    return this.status.equals(ServerResponseCode.SUCCESS); }

  /**
   * create a server success response with message
   *
   * @param msg response message as user defined
   * @param <T> object type
   * @return server response object
   */
  public static <T> ServerResponse<T> createSuccess(String msg) {
    return new ServerResponse<T>(ServerResponseCode.SUCCESS, msg);
  }

  /**
   * create a server success response with message
   *
   * @param object response object as user defined
   * @param <T> object type
   * @return server response object
   */
  public static <T> ServerResponse<T> createSuccess(T object) {
    return new ServerResponse<T>(ServerResponseCode.SUCCESS, object);
  }

  /**
   * create a server success response with message and object
   *
   * @param msg response message as user defined
   * @param object response object as user defined
   * @param <T> object type
   * @return server response object
   */
  public static <T> ServerResponse<T> createSuccess(String msg, T object) {
    return new ServerResponse<T>(ServerResponseCode.SUCCESS, msg, object);
  }

  /**
   * create a server error response with message
   *
   * @param msg response message as user defined
   * @param <T> object type
   * @return server response object
   */
  public static <T> ServerResponse<T> createError(String msg) {
    return new ServerResponse<T>(ServerResponseCode.ERROR, msg);
  }

  /**
   * create a server error response with message
   *
   * @param object response object as user defined
   * @param <T> object type
   * @return server response object
   */
  public static <T> ServerResponse<T> createError(T object) {
    return new ServerResponse<T>(ServerResponseCode.ERROR, object);
  }

  /**
   * create a server error response with message and object
   *
   * @param msg response message as user defined
   * @param object response object as user defined
   * @param <T> object type
   * @return server response object
   */
  public static <T> ServerResponse<T> createError(String msg, T object) {
    return new ServerResponse<T>(ServerResponseCode.ERROR, msg, object);
  }
}
